package com.dd.cloud.common.config.mail;
import com.dd.cloud.common.config.mail.MailProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * 邮件发送工具类
 * @Author: czg
 * @Date: 2022-04-08 10:15:15
 */
@Component
@ConditionalOnBean({MailProperties.class, JavaMailSender.class})
public class MailUtilConfig {
    @Autowired
    private JavaMailSender MailSender;

    @Autowired
    private MailProperties mailProperties;

    /**
     * 发送简单消息
     * @param to 目标邮箱
     * @param text 邮件内容
     * @param subject 邮件主题
     * @return //1发送成功 0发送失败
     */
    public int sendSimpleMail(String to,String text,String subject){
        SimpleMailMessage simpleMailMessage=new SimpleMailMessage();
        simpleMailMessage.setFrom(mailProperties.getUsername());
        simpleMailMessage.setTo(to);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(text);

        try {
            System.out.println(simpleMailMessage);
            System.out.println(MailSender);
            MailSender.send(simpleMailMessage);
            return 1;
        } catch (MailException e) {
            e.printStackTrace();
            return 0;
        }

    }

    /**
     * 发送html格式文本
     * @param to 目标邮箱
     * @param text 邮件内容,带标签的html内容
     * @param subject 邮件主题
     * @return
     */
    public int sendHtmlMail(String to,String subject,String text){
        MimeMessage mimeMessage=MailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper=new MimeMessageHelper(mimeMessage,true);
            mimeMessageHelper.setFrom(mailProperties.getUsername());
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.setText(text,true);//html格式为true
            MailSender.send(mimeMessage);
            return 1;
        } catch (MessagingException e) {
            e.printStackTrace();
            return 0;
        }


    }

    /**
     * 发送带附件邮件
     * @param to 目标邮箱
     * @param text 邮件内容
     * @param subject 邮件主题
     * @param path 附件地址
     * @return
     */
    //
    public int sendAttachMail(String to,String text,String subject,String path){
        MimeMessage mimeMessage=MailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper=new MimeMessageHelper(mimeMessage,true,"utf-8");
            mimeMessageHelper.setFrom(mailProperties.getUsername());
            mimeMessageHelper.setText(text,true);
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.setTo(to);
            FileSystemResource fileSystemResource=new FileSystemResource(new File(path));
            String fileName=path.substring(path.lastIndexOf(File.separator));
            mimeMessageHelper.addAttachment(fileName,fileSystemResource);
            MailSender.send(mimeMessage);
            return 1;

        } catch (MessagingException e) {
            e.printStackTrace();
            return 0;
        }

    }

    /**
     * 发送带静态图片邮件
     * @param to 目标邮箱
     * @param text 邮件内容
     * @param subject 邮件主题
     * @param path 图片地址
     * @param id
     * @return
     */
    public int sendPicMail(String to,String text,String subject,String path,String id){
        MimeMessage mimeMessage=MailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper=new MimeMessageHelper(mimeMessage,true);
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setFrom(mailProperties.getUsername());
            mimeMessageHelper.setText(text,true);
            mimeMessageHelper.setSubject(subject);
            FileSystemResource fileSystemResource=new FileSystemResource(new File(path));
            mimeMessageHelper.addInline(id,fileSystemResource);
            MailSender.send(mimeMessage);
            return 1;


        } catch (MessagingException e) {
            e.printStackTrace();
            return 0;
        }

    }

    /**
     * 发送模板文件
     * @param to
     * @param subject
     * @param name
     * @return
     */
//    public int sendTemplateMail(String to,String subject,String name){
//        //创建邮件正文
//        Context context = new Context();
//        context.setVariable("name", name);
//        String emailContent = templateEngine.process("MailTemplate", context);
//        int result=sendHtmlMail(to,subject,emailContent);
//        return result;
//    }
}
